package com.yanqu.road.server.cmd.activity.huashigang;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.huashigang.HuaShiGangProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.huashigang.HuaShiGangModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.huashigang.HuaShiGangMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

/**
 * @Author wpj
 * @Date 2020/10/5
 */
@Cmd(code = GameProtocol.S_HUA_SHI_GANG_CHOOSE_NEXT_POINT, desc = "获取下一个地点")
public class ChooseNextPointCmd extends AbstractCommand {

    int x;
    int y;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        HuaShiGangProto.ChooseNextPointReqMsg msg = HuaShiGangProto.ChooseNextPointReqMsg.parseFrom(packet.getBytes());
        x = msg.getX();
        y = msg.getY();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ret = HuaShiGangMgr.getActivityInfoRet(player);
        if (ret != 0) {
            return;
        }

        // 活动期间内才能操作
        ActivityInfo activityInfo = HuaShiGangMgr.getActivityInfo();
        if (!ActivityMgr.activityInTime(activityInfo)) {
            ret = GameErrorCode.E_HUA_SHI_GAG_NOT_IN_FIGHT_TIME;
            return;
        }

        if (!HuaShiGangMgr.inOperateTime(activityInfo)) {
            ret = GameErrorCode.E_HUASHIGANG_NOT_OPERATE_TIME;
            return;
        }

        long goldenKeyNum = player.getModule(BagModule.class).getUserBagItemCount(HuaShiGangMgr.getHuaShiGangGoldKeyId());

        //门客总赚钱（算上了天赋技能加成，计算BOSS血量使用）
        long totalAbility = player.getModule(HuaShiGangModule.class).getPatronsAbility();

        HuaShiGangProto.CrossChooseNextPointReqMsg.Builder builder = HuaShiGangProto.CrossChooseNextPointReqMsg.newBuilder();
        builder.setX(x);
        builder.setY(y);
        builder.setActivityId(activityInfo.getActivityId());
        builder.addAllPatronsId(player.getModule(PatronsModule.class).getUserPatronsMap().keySet());
        builder.setGoldenKeyNum((int)goldenKeyNum);
        builder.setPlayerBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        builder.setTotalAbility(totalAbility);
        player.sendPacket(CrossProtocol.C2_HUA_SHI_GANG_CHOOSE_NEXT_POINT, builder);

    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            HuaShiGangProto.ChooseNextPointRespMsg.Builder builder = HuaShiGangProto.ChooseNextPointRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_HUA_SHI_GANG_CHOOSE_NEXT_POINT, builder);
        }
    }
}
